namespace ghost {
	export class ToucherCtrl extends Laya.EventDispatcher{	
		private static _instance:ToucherCtrl;
		public static getInstance():ToucherCtrl
		{
			if(ToucherCtrl._instance == null)
				new ToucherCtrl;
			return ToucherCtrl._instance;
		}	
		private _tarArray:Laya.Sprite[];
		private _classArray:any[];
		private _touchBeginTarget:Laya.Sprite;
		private _touchBeginX:number;
		private _touchBeginY:number;	
		private _propArr:any[];
		private _stopTouchEvent:boolean;
		private _isDragMove:boolean;
		private _touchId:number;
		public constructor() {
			super();			
			let s = this;			
			ToucherCtrl._instance = s;
			s._tarArray = [];
			s._classArray = [];
			s._propArr = [];	
			s._stopTouchEvent = true;
			//优先执行toucher监听，所以监听改成捕获模式
			Laya.stage.on(Laya.Event.MOUSE_DOWN,s,s.touch);
			Laya.stage.on(Laya.Event.MOUSE_MOVE,s,s.touch);
			Laya.stage.on(Laya.Event.MOUSE_UP,s,s.touch);
			Laya.stage.on(Laya.Event.CLICK,s,s.touch);
		}
		/**当拖拽产生时，是否屏蔽鼠标点击事件*/
		public whenDragStopTouch(val:boolean)
		{
			let s = this;
			s._stopTouchEvent = val;
		}
		/**添加需要监听对象的属性key的对象列表
		 * @param arr 对象数组
		 * @param key 对象具有的key属性进行匹配，为空则直接匹配对象
		*/
		public addPropList(arr:any[],key:string=null):void
		{
			let s = this;
			s._propArr.push(arr,key);
		}
		/**移除需要监听对象的属性key的对象列表*/
		public removePropList(arr:any[],key:string):void
		{
			let s = this;
			let ind:number,ind2:number;
			ind = s._propArr.indexOf(arr);
			ind2 = s._propArr.indexOf(key);
			if(ind == ind2 - 1)
				s._propArr.splice(ind,2);
		}
		/**移除所有监听的对象列表*/
		public removeAllProlList():void
		{
			let s = this;
			s._propArr.length = 0;
		}
		/**添加监听的类型*/
		public addClass(cls:any):void
		{
			let s =this;
			if(s._classArray.indexOf(cls) == -1)
				s._classArray.push(cls);
		}
		/**移除监听的对象*/
		public removeClass(cls:any):void
		{
			let s =this;
			let ind:number = s._classArray.indexOf(cls);
			if(ind > -1)
				s._classArray.splice(ind,1);
		}
		/**移除所有的类型监听*/
		public removeAllClass():void
		{
			let s = this;
			s._classArray.length = 0;
		}
		/**添加监听的对象*/
		public addTarget(tar:Laya.Sprite):void
		{
			let s =this;
			if(tar == null)return;
			if(s._tarArray.indexOf(tar) == -1)
				s._tarArray.push(tar);
		}
		/**移除监听的对象*/
		public removeTarget(tar:Laya.Sprite):void
		{
			let s =this;
			if(tar == null)return;
			let ind:number = s._tarArray.indexOf(tar);
			if(ind > -1)
				s._tarArray.splice(ind,1);
		}
		/**移除所有的对象监听*/
		public removeAllTarget():void
		{
			let s = this;
			s._tarArray.length = 0;
		}
		/**移除所有监听*/
		public removeAll():void
		{
			let s = this;
			s.removeAllTarget();
			s.removeAllClass();
			s.removeAllProlList();
		}
		/**是否能触摸*/
		public canTouch(p:Laya.Sprite):boolean
		{
			let ind:number,len:number;
			let s = this;
			if(CommonUtil.GYIs(p,Laya.Sprite))
			{
				len = s._classArray.length;
				while(--len > -1)
				{
					if(CommonUtil.GYIs(p,s._classArray[len]))
						return true;
				}
				// ind = s._classArray.indexOf(p.constructor);
				// if(ind > -1)
				// {				
				// 	return true;
				// }
				ind = s._tarArray.indexOf(p);
				if(ind > -1)
				{				
					return true;
				}
				let j:number,len2:number,len3:number;
				let arr:any[];
				len2 = s._propArr.length;
				for(j=0;j<len2;j+=2)
				{								
					arr = s._propArr[j];
					len3 = arr.length;
					while(--len3 > -1)
					{
						if(s._propArr[j+1] && arr[len3][s._propArr[j+1]] == p || s._propArr[j+1] == null && arr[len3] == p)
						{
							return true;							
						}
					}							
				}
			}
			return false;
		}
		private touch(e:Laya.Event):void
		{
			let s = this;
			let len:number,ind:number,j:number,len2:number,k:number,len3:number;
			let p:Laya.Sprite,sp:Laya.Sprite;
			let arr:any[];
			let flag:boolean;			
			if(e.type == Laya.Event.MOUSE_MOVE)
			{
				if(s._touchId == e.touchId)									
					s._isDragMove = true;
			}
			p = e.target;
			while(p && (<any>Laya.stage) != p)
			{
				if(CommonUtil.GYIs(p,Laya.Sprite))
				{
					len = s._classArray.length;
					while(--len > -1)
					{
						if(CommonUtil.GYIs(p,s._classArray[len]))
						{
							sp = p;
							flag = true;				
						}
					}
					// ind = s._classArray.indexOf(p.constructor);
					// if(ind > -1)
					// {				
					// 	sp = p;		
					// 	flag = true;
					// }
					if(!flag)
					{
						ind = s._tarArray.indexOf(p);
						if(ind > -1)
						{				
							sp = p;			
							flag = true;
						}
					}
					if(!flag)
					{						
						len2 = s._propArr.length;
						for(j=0;j<len2;j+=2)
						{								
							arr = s._propArr[j];
							len3 = arr.length;
							while(--len3 > -1)
							{
								if(s._propArr[j+1] && arr[len3][s._propArr[j+1]] == p || s._propArr[j+1] == null && arr[len3] == p)
								{		
									sp = arr[len3];						
									flag = true;
									break;
								}
							}							
						}
						
					}
					if(flag)
					{						
						if(e.type == Laya.Event.MOUSE_DOWN)		
						{							
							s._touchBeginTarget = sp;
							s._touchBeginX = sp.mouseX;
							s._touchBeginY = sp.mouseY;	
							s._touchId = e.touchId;
							s._isDragMove = false;
						}
						if(!s._stopTouchEvent || !s._isDragMove)
						{
							s.event(s._isDragMove?Laya.Event.DRAG_MOVE:e.type,{
								touchPointID:e.touchId,
								target:e.target,
								currentTarget:sp,
								currentTargetMouseX:sp.mouseX,
								currentTargetMouseY:sp.mouseY,
								mouseX:e.target.mouseX,
								mouseY:e.target.mouseY
							});
						}
						
						if(e.type == Laya.Event.MOUSE_UP)		
						{							
							s.mouseUp();
						}
						return;
					}
				}				
				p = <any>p.parent;
			}
			s.event(e.type);
		}
		public mouseUp():void
		{
			let s = this;
			s._touchBeginTarget = null;
			s._touchBeginX = NaN;
			s._touchBeginY = NaN;	
			s._touchId = NaN;
			s._isDragMove = false;
		}
		
		/**上次按下的监听对象的坐标X*/
		public get touchBeginX():number
		{
			return this._touchBeginX;
		}
		/**上次按下的监听对象的坐标Y*/
		public get touchBeginY():number
		{
			return this._touchBeginY;
		}
		/**上次按下的监听对象*/
		public get touchBeginTarget():Laya.Sprite
		{
			return this._touchBeginTarget;
		}
	}
}